package com.longge;

import java.util.Arrays;
import java.util.PriorityQueue;

public class AdvantageCount {

    public int[] advantageCount(int[] nums1, int[] nums2) {

        Arrays.sort(nums1);
        PriorityQueue<int[]> maxPq = new PriorityQueue<>((o1, o2) -> o2[1] - o1[1]);
        for (int i = 0; i < nums2.length; i++) {
            maxPq.offer(new int[]{i, nums2[i]});
        }
        int[] res = new int[nums2.length];
        int left = 0, right = nums2.length - 1;
        while (!maxPq.isEmpty()) {
            int[] maxNums = maxPq.poll();
            int i = maxNums[0];
            int max = maxNums[1];
            if (nums1[right] > max) {
                res[i] = nums1[right];
                right--;
            } else {
                res[i] = nums1[left];
                left++;
            }
        }
        return res;
    }
}
